﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Element.Net.Controls
{
    public class ElementTextBox : TextBox
    {
        public ElementTextBox()
        {
            BorderStyle = BorderStyle.None;
        }

        /// <summary>
        /// 重绘背景
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
            Graphics graphics = pevent.Graphics;
            Rectangle rectangle = new Rectangle(-1, -1, Width, Height);
            DrawRectangle(graphics, rectangle, Color.Transparent, 1f);
        }

        /// <summary>
        /// 重绘按钮
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            Graphics graphics = pevent.Graphics;
            Rectangle rectangle = new Rectangle(-1, -1, Width, Height);
            DrawRectangle(graphics, rectangle, Color.Black, 15);
            Rectangle rect = new Rectangle(0, 0, Width - 1, Height);
            graphics.DrawPath(new Pen(Color.Blue), GetGraphicsPath(rect, 15));
        }

        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        private GraphicsPath GetGraphicsPath(Rectangle rect, float radius)
        {
            GraphicsPath GraphicsPath = new GraphicsPath();
            // 左上角
            GraphicsPath.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
            // 右上角
            GraphicsPath.AddArc(rect.Width - radius, rect.Y, radius, radius, 270, 90);
            // 右下角
            GraphicsPath.AddArc(rect.Width - radius, rect.Height - radius, radius, radius, 0, 90);
            // 左下角
            GraphicsPath.AddArc(rect.X, rect.Height - radius, radius, radius, 90, 90);
            GraphicsPath.CloseAllFigures();
            return GraphicsPath;
        }

        /// <summary>
        /// 高质量绘制矩形
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="rectangle"></param>
        /// <param name="color"></param>
        private void DrawRectangle(Graphics graphics, Rectangle rectangle, Color color, float radius)
        {
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            GraphicsPath graphicsPath = GetGraphicsPath(rectangle, radius);
            SolidBrush solidBrush = new SolidBrush(color);
            graphics.FillPath(solidBrush, graphicsPath);
        }
    }
}
